home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 3 / Info_Mac_1994-01.iso / Development / Source / Finger Server Source / get_finger_info.cp < prev    next >
Text File  |  1993-10-05  |  7KB  |  298 lines

  1. /*
  2.  * return the current system finger info
  3.  * by Aaron Wohl n3liw+@cmu.edu
  4.  * 412-268-5032 / 412-731-3691
  5.  */
  6.  
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include <GestaltEQU.h>
  10. #include <Processes.h>
  11. #include <string.h>
  12. #include <Packages.h>
  13. #include "fcb.h"
  14.  
  15. #include "sip_interface.h"
  16.  
  17. #define dst_printf(xx_arg) do { sprintf xx_arg ; dst+=strlen(dst); } while(0)
  18.  
  19. #define BYTE0(xx_arg) ((xx_arg)&0x0FF)
  20. #define BYTE1(xx_arg) BYTE0(xx_arg>>8)
  21.  
  22. #define BSTR_LEN            (256)        //general string buffer length
  23. #define    MAX_DISKS            (10)        //limit of number of disks in bug report
  24. #define CLEAR_PARITY(xx)    ((xx)&0x7F)
  25. #define DEL_CH                0x7F
  26. #define MAX_DISK_LEN        (17)        //maxmimum length of one disk name
  27. #define KSIZE                (1024)
  28. #define MIN_DISK_SIZE        (2000000.0)        //ignore disks smaller than this
  29.  
  30. /*
  31.  * return a gestalt long or zero if not found
  32.  */
  33. static long gestalt_read(long selector)
  34. {
  35.     long response;
  36.     int iErr = Gestalt(selector, &response);
  37.     if ( iErr )
  38.         return 0;
  39.     return response;
  40. }
  41.  
  42. /*
  43.  * count fcb blocks that are in use
  44.  */
  45. static long count_fcbs_inuse(int num_fcbs,FCBrec_pt fcbs)
  46. {
  47.     register int result=0;
  48.     while(--num_fcbs >=0)
  49.       if((fcbs++)->fcbFlags!=0)
  50.         result++;
  51.     return result;
  52. }
  53.  
  54. /*
  55.  * print the open files
  56.  */
  57. static char *print_fcbs(char *dst,char *limit,int num_fcbs,FCBrec_pt fcbs)
  58. {
  59.     for(;num_fcbs>0;num_fcbs--, fcbs++)
  60.       if(fcbs->fcbFlags!=0) {
  61.         int len=(fcbs->fcbCName[0]&0xFF);
  62.         if(len>0) {
  63.             dst_printf((dst,"File "));
  64.             dst_printf((dst,"%ld,%ld,",fcbs->fcbCrPs,fcbs->fcbEOF));
  65.               memcpy(dst,fcbs->fcbCName+1,len);
  66.               dst+=len;
  67.               *dst++ = '\n';
  68.           if((dst-limit)>=0)
  69.               break;
  70.           }
  71.      }
  72.      *dst =0;
  73.      return dst;
  74. }
  75.  
  76. /*
  77.  * report on file control block usage
  78.  */
  79. static char *fcb_report(char *dst,char *limit)
  80. {
  81.     char **xxFCBSPtr = (char **)0x34e;
  82.     unsigned short *fcb_space=(unsigned short *) (*xxFCBSPtr);
  83.     FCBrec_pt fcbs=((FCBrec_pt)(fcb_space+1));
  84.     long num_fcbs= *fcb_space;
  85.     num_fcbs/=sizeof(FCBrec);
  86.     dst_printf((dst,"Files %ld/%ld\n",count_fcbs_inuse(num_fcbs,fcbs),num_fcbs));
  87.     dst=print_fcbs(dst,limit,num_fcbs,fcbs);
  88.     return dst;
  89. }
  90.  
  91. /*
  92.  * convert illegal characters to dots so so they don't mess up the guardian logs
  93.  */
  94. static void clean_file_name(unsigned char *buf,int len)
  95. {
  96.     register unsigned char ch;
  97.     while (len-- > 0) {
  98.         ch=CLEAR_PARITY(*buf);
  99.         if((ch<' ')||
  100.             (ch==DEL_CH)||
  101.             (ch==':')||
  102.             (ch==','))
  103.                   ch='.';
  104.         *buf++ = ch;
  105.     }
  106.     *buf++ =0;
  107. }
  108.  
  109. /*
  110.  * is the passed volume an appleshare server?
  111.  */
  112. static int is_aftp_volume(short vnum)
  113. {
  114.     int err;
  115.     HParamBlockRec pblk;
  116.     GetVolParmsInfoBuffer dst;
  117.     memset(&pblk,0,sizeof(pblk));
  118.     pblk.ioParam.ioVRefNum=vnum;
  119.     pblk.ioParam.ioBuffer= (Ptr)&dst;
  120.     pblk.ioParam.ioReqCount=sizeof(dst);
  121.     err = PBHGetVolParms(&pblk,FALSE);
  122.     if(err!=0)
  123.         return FALSE;
  124.     if((dst.vMAttrib & (1L<<bHasExtFSVol) )!=0)
  125.         return TRUE;
  126.     return FALSE;
  127. }
  128.  
  129. /*
  130.  * get info about a particular disk drive
  131.  */
  132. static char *vol_dinfo(char *dst,int dnum)
  133. {
  134.     HParamBlockRec pblk;
  135.     int err;
  136.     unsigned char buf[BSTR_LEN];
  137.     int len;
  138.     double fdisk_size;
  139.     double fdisk_free;
  140.     long percent_used;
  141.     long disk_size;
  142.     long disk_free;
  143.  
  144.     memset(&pblk,0,sizeof(pblk));
  145.  
  146.     pblk.volumeParam.ioVolIndex= dnum;
  147.     pblk.volumeParam.ioNamePtr=(StringPtr)buf;
  148.     err=PBHGetVInfo(&pblk,TRUE);
  149.     if(err!=0)
  150.         return dst;
  151.  
  152.     if(is_aftp_volume(pblk.volumeParam.ioVRefNum))
  153.         return dst;
  154.  
  155.     len=buf[0];
  156.     clean_file_name(&buf[1],len);
  157.     buf[MAX_DISK_LEN]=0;        /*possibly truncate file name*/
  158.  
  159.     fdisk_size=pblk.volumeParam.ioVNmAlBlks;
  160.     fdisk_free=pblk.volumeParam.ioVFrBlk;
  161.     fdisk_size*=((unsigned)pblk.volumeParam.ioVAlBlkSiz);
  162.     fdisk_free*=((unsigned)pblk.volumeParam.ioVAlBlkSiz);
  163.     if(fdisk_size<=MIN_DISK_SIZE)
  164.         return dst;
  165.     percent_used=(((fdisk_size-fdisk_free)/fdisk_size)*100.0);
  166.     if(percent_used<0)
  167.         percent_used=0;
  168.     else if (percent_used>100)
  169.         percent_used=100;
  170.     fdisk_size/=KSIZE;
  171.     fdisk_free/=KSIZE;
  172.     disk_size=fdisk_size;
  173.     disk_free=fdisk_free;
  174.  
  175. //        pblk.volumeParam.ioVCrDate,
  176. //        pblk.volumeParam.ioVFSID,
  177.  
  178.     dst_printf((dst,"Disk %3ld%% %7ld %7ld %s\n",
  179.         percent_used,
  180.         disk_size,
  181.         disk_free,
  182.         &buf[1]));
  183.     return dst;
  184. }
  185.  
  186. /*
  187.  * report online disks
  188.  */
  189. static char *report_disks(char *dst,char *limit)
  190. {
  191.     int i;
  192.     for(i=1;i<MAX_DISKS;i++) {
  193.         if ((dst-limit)>=0)
  194.             break;
  195.         dst=vol_dinfo(dst,i);
  196.         }
  197.     return dst;
  198. }
  199.  
  200. #define MIN         ((unsigned long)(60L))
  201. #define HOUR        (MIN*60)
  202. #define DAY            (24*HOUR)
  203. #define TICKS_PER_SECOND ((unsigned long)(60L))
  204.  
  205. /*
  206.  * convert a time in seconds to X-hh:mm:ss
  207.  */
  208. static void relative_time(char *dst,unsigned long atime)
  209. {
  210.     unsigned long days;
  211.     unsigned long hours;
  212.     unsigned long mins;
  213.     unsigned long secs;
  214.  
  215.     atime/=TICKS_PER_SECOND;
  216.     days=atime/DAY;
  217.     atime%=DAY;
  218.     hours=atime/HOUR;
  219.     atime%=HOUR;
  220.     mins=atime/MIN;
  221.     atime%=MIN;
  222.     secs=atime;
  223.     sprintf(dst,"%2ld-%02ld:%02ld:%02ld",days,hours,mins,secs);
  224. }
  225.  
  226. /*
  227.  * display running processes
  228.  */
  229. static char *proc_report(char *dst,char *limit)
  230. {
  231.     ProcessInfoRec infoRec;
  232.     ProcessSerialNumber process;
  233.     FSSpec aFSSpec;
  234.     int len;
  235.     unsigned char proc_name[32];
  236.     char cpu_time[BSTR_LEN];
  237.     char start_time[BSTR_LEN];
  238.     process.highLongOfPSN = 0;
  239.     process.lowLongOfPSN = kNoProcess;     // start from the beginning
  240.     infoRec.processInfoLength = sizeof(ProcessInfoRec);
  241.     infoRec.processName = (StringPtr) proc_name;
  242.     infoRec.processAppSpec = &aFSSpec;
  243.     dst_printf((dst,"# start-time cpu-used heap-size heap-free name\n"));
  244.     while (GetNextProcess(&process)==0) {
  245.         if((dst-limit)>=0)
  246.             break;
  247.         if ( GetProcessInformation(&process,&infoRec) != 0)
  248.             continue;
  249.         len=proc_name[0];
  250.         clean_file_name(&proc_name[1],len);
  251.         proc_name[sizeof(proc_name)-1]=0;        /*possibly truncate file name*/
  252.         relative_time(cpu_time,infoRec.processActiveTime);
  253.         relative_time(start_time,TickCount()-infoRec.processLaunchDate);
  254.         dst_printf((dst,"Proc %s %s %5ld %5ld %s\n",
  255.             start_time,
  256.             cpu_time,
  257.             infoRec.processSize/KSIZE,
  258.             infoRec.processFreeMem/KSIZE,
  259.             proc_name+1));
  260.     }
  261.     return dst;
  262. }
  263.  
  264. /*
  265.  * report current data and time
  266.  */
  267. static char *report_time(char *dst,char *limit)
  268. {
  269.     unsigned long secs;
  270.     Str255    todayStr;
  271.     Str255    time_string;
  272.     GetDateTime(&secs);
  273.     IUDateString( secs, longDate, todayStr );    /* text w/o abbreviations */
  274.     IUTimeString(secs,FALSE,time_string);
  275.     dst_printf((dst,"Time %#s %#s\n",todayStr,time_string));
  276.     return dst;
  277. }
  278.  
  279. /*
  280.  * return info about this system
  281.  */
  282. void get_finger_info(char *dst,char *limit)
  283. {
  284.     dst_printf((dst,"FingerServ %ld.%ld ",(long)FSERV_MAJ,(long)FSERV_MIN));
  285.     {
  286.         long sys_vers=gestalt_read('sysv');
  287.         long sys_maj=BYTE1(sys_vers);
  288.         long sys_min=BYTE0(sys_vers);
  289.         dst_printf((dst,"Sys %ld.%ld, ",sys_maj,sys_min));
  290.     }
  291.     dst_printf((dst,"MacTCP v%ld\n",gestalt_read('mtcp')));
  292.     dst=report_time(dst,limit);
  293.     dst_printf((dst,"# %%used sizeK freeK name\n"));
  294.     dst=report_disks(dst,limit);
  295.     dst=proc_report(dst,limit);
  296.     dst=fcb_report(dst,limit);
  297. }
  298.